package com.graph.base;

import java.util.List;
import java.util.Set;

public class Modularity {
	/**
	 * 局部中心点算法中的社区衡量度
	 * @Description:TODO
	 * @param vertexInterfaceSet
	 * @return
	 * @author: wujiang
	 * @time:2017-3-5 下午12:56:34
	 */
	 public double modularityR(Set<VertexInterface> vertexInterfaceSet){
		 if(vertexInterfaceSet == null){
		 	 return -1f;
		 }
		 if(vertexInterfaceSet.size() <= 1){
		 	 return 0f;
		 }
		 double bIn = 0;
		 double bOut = 0;
		 int i = 0;
		 int j = 0;
		 for (VertexInterface vertexInterface : vertexInterfaceSet) {
		 	 if("25".equals(vertexInterface.getName())){
				 for(Edge edge : vertexInterface.getEdgeList()){
					 VertexInterface end = edge.getEndVertex();
					 System.out.println("终点："+end.getName());
					 if(vertexInterfaceSet.contains(end)){
						 i++;
					 }else{
						 j++;
					 }
				 }
				 System.out.println(i+" "+j);
			 }
			 for(Edge edge : vertexInterface.getEdgeList()){
			 	 VertexInterface end = edge.getEndVertex();
				 if(vertexInterfaceSet.contains(end)){
					 bIn++;
				 }else{
					 bOut++;
				 }
			}
		}
		 System.out.println("内部边："+bIn);
		 System.out.println("外部边："+bOut);
		//因为是双向边
		bIn = bIn / 2;
		return  bIn / (bIn + bOut);
	 }

	/**
	 * 求社区的局部模块化度
	 * @param vertexInterfaceSet 社区
	 * @param vertexInterfaceList 孤立节点
	 * @return
     */
	public double modularityR1(Set<VertexInterface> vertexInterfaceSet, List<VertexInterface> vertexInterfaceList){
		if(vertexInterfaceSet == null){
			return -1f;
		}
		if(vertexInterfaceSet.size() <= 1){
			return 0f;
		}
		double bIn = 0;
		double bOut = 0;
		for (VertexInterface vertexInterface : vertexInterfaceSet) {
			for(Edge edge : vertexInterface.getEdgeList()){
				VertexInterface end = edge.getEndVertex();
				if(vertexInterfaceSet.contains(end)){
					bIn++;
				}else{
					if(!vertexInterfaceList.contains(end)){
						bOut++;
					}
				}
			}
		}
		System.out.println("内部边："+bIn);
		System.out.println("外部边："+bOut);
		//因为是双向边
		bIn = bIn / 2;
		return  bIn / (bIn + bOut);
	}
}
